package com.hongminzheng.openapi.controller;

import com.github.pagehelper.PageInfo;
import com.hongminzheng.openapi.bean.Result;
import com.hongminzheng.openapi.bean.TableData;
import com.hongminzheng.openapi.pojo.SysParam;
import com.hongminzheng.openapi.service.SysParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hmz
 * @version v1.0
 * @data 2020/09/29
 **/
@RestController
@RequestMapping("/sysParam")
public class SysParamController {
    /**
     * 创建服务层对象
     */
    @Autowired
    private SysParamService sysParamService;

    /**
     * 增加
     * @param sysParam
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result insertSysParam(SysParam sysParam){
        try {
            sysParamService.insert(sysParam);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            return new Result(false, "添加失败");
        }
    }
    /**
     * 更新
     * @param sysParam
     * @return
     */
    @RequestMapping("/update")
    public Result updateSysParam(SysParam sysParam){
        try {
            sysParamService.updateByPrimaryKey(sysParam);
            return new Result(true, "更新成功");
        } catch (Exception e) {
            return new Result(false, "更新失败");
        }
    }

    /**
     * 分页查询所有
     * @param page
     * @param limit
     * @return sysParamTableData
     */
    @GetMapping("/page")
    @ResponseBody
    public TableData<SysParam> findLayuiPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, String name, Integer state){
        SysParam sysParam = new SysParam();
        sysParam.setName(name);
        sysParam.setState(state);

        TableData<SysParam> sysParamTableData = new TableData<>();
        PageInfo<SysParam> sysParamPageInfo = sysParamService.findPage(page, limit,sysParam);
        sysParamTableData.setCount(sysParamPageInfo.getTotal());
        sysParamTableData.setData(sysParamPageInfo.getList());
        return sysParamTableData;
    }

    @PostMapping("/dels")
    @ResponseBody
    public Result dels(@RequestBody List<SysParam> sysParamList) {
        //创建返回对象
        Result result = new Result();
        //创建循环，调用删除方法
        sysParamList.forEach(sysParam -> {
            sysParamService.deleteByPrimaryKey(sysParam.getId());
        });
        result.setStatus(true);
        result.setMessage("批量删除成功");
        return result;
    }


}
